<?php
/**
 * File: ExamController.php
 * User: lyc
 * Date: 16/3/28
 * Time: 下午12:11
 */

namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Auth;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Input;
use PHPExcel;
use PHPExcel_IOFactory;
use DB;
use Hash;
use Illuminate\Support\Str;
use Cache;
use Illuminate\Support\Facades\File;

class ExamController extends Controller
{
    
    
    
    //验证学生信息
    public function vaildStuInfo(Request $request)
    {
        $askCode = $request->input('askCode');
        $stuname = $request->input('stuname');
        $stunum = $request->input('stunum');
        $index = 1;
        $expiresTime = Carbon::now()->addMinutes(600);
        $stuLogging = array();
        $isLogging = 0; //登录标记
        if(Cache::has($askCode."_option"))
        {
            $options = Cache::get($askCode."_option");
            $mode = $options['mode'];
        }else{
            $mode = 1;
        }
        //判断是否已经登录,并返回题号
        if (Cache::has($askCode . '_stuLogged')) {
            $stuLogging = Cache::get($askCode . '_stuLogged');
            if (in_array($stuname, $stuLogging)) {
                if (Cache::has($askCode . '_nowIndex')) {
                    $index = Cache::get($askCode . '_nowIndex');
                } else {
                    $index = 1;
                }
                $isLogging = 1;
                return response()->json(["state" => '4', "index" => $index, "mode"=>$mode,"error" => "你已重复登录成功", "mode"=>$mode]);
            }
        }

        if ($isLogging == 0) {
            //判断学生askCode和学生名单是否匹配
            if (Cache::has($askCode . '_stu')) {
                $flag = 0;
                $stuAll = Cache::get($askCode . '_stu');
                //判断学生学号是否在数组种
                if (isset($stuAll[$stunum]) && $stuAll[$stunum] == $stuname) {
                    $flag = 1;
                    array_push($stuLogging, $stuname);
                    Cache::put($askCode . '_stuLogged', $stuLogging, $expiresTime);
                } else {
                    $flag = 0;
                }

                if ($flag == 0) {
                    return response()->json(["state" => '2', "error" => '学号或姓名不正确', "mode"=>$mode]);
                } else {
                    if (Cache::has($askCode . '_nowIndex')) {
                        $index = Cache::get($askCode . '_nowIndex');
                    } else {
                        $index = 1;
                    }
                    return response()->json(["state" => '1', "index" => $index, "mode"=>$mode]);
                }
            } else {
                return response()->json(["state" => '3', "error" => 'Code错啦', "mode"=>$mode]);
            }
        }
    }


    //开始答题
    public function startAnswer(Request $request)
    {
        $askCode = $request->input('askCode');
        $start = $request->input('start');
        $index = $request->input('index');
        $expiresAt2 = Carbon::now()->addMinutes(20);
        if ($start == 1) {
            Cache::put($askCode . '_isStart_' . $index, 1, $expiresAt2);
        } else {
            Cache::put($askCode . '_isStart_' . $index, 0, $expiresAt2);
        }
    }

    public static $TEACHER_CONTROLLER_MODE = 1;
    public static $FREE_ASK_MODE = 2;
    
    //接收学生的答案
    public function answerFromStu(Request $request)
    {
        $askCode = $request->input('askCode');
        $index = $request->input('index');
        $idx = 0;
        $mode = 1;

        if(Cache::has($askCode."_option"))
        {
            $options = Cache::get($askCode."_option");
            $mode = $options['mode'];
            //var_dump($options);
        }
        //获取当前题号
        if (Cache::has($askCode . '_nowIndex')) {
            $idx = Cache::get($askCode . '_nowIndex');
        }

        //判断学生答题是否与教师端题目相同,不同则返回正确题号
        if($mode == 1) {
            if (Cache::has($askCode . '_nowIndex')) {
                $nowIndex = Cache::get($askCode . '_nowIndex');
            }else{
                $nowIndex = 0;
            }
            if ($index != $nowIndex && $nowIndex != 0) {
                return response()->json(['state' => '5', 'nowIndex' => $nowIndex, "mode"=>$mode]);
            } else if($nowIndex < 1){
                return response()->json(['state' => '8','error' => '老师还没有选择题目!' , "mode"=>$mode]);
            } else if($index == $nowIndex)
            {
                
            }
            else{
                return response()->json(['state' => '6', 'error' => '你的Code错了', "mode"=>$mode]);
            }
        }


            //判断是否是答题时间
        if (Cache::get($askCode . '_isStart_' . $idx) == 0 && $mode == 1 ) {
            return response()->json(["state" => '3', "error" => '现在还不是答题时间']);
        }



        //构造回答问题数组
        $answer = $request->input('answer');
        $moreAnswer = $request->input('moreAnswer');
        $stuid = $request->input('stunum');
        $stuname = $request->input('stuname');
        $hasStudent = 0;
        $answerInfo = array();
        $answerInfo['askCode'] = $askCode;
        $answerInfo['stuid'] = $stuid;
        $answerInfo['index'] = $index;
        $answerInfo['chooses'] = $answer;
        //$answerInfo['answer'] = $answer;
        $answerInfo['moreAnswer'] = $moreAnswer;
        $answerInfo['answerTime'] = time() + microtime();//date('y-m-d h:i:s',time());
        $expiresTime = Carbon::now()->addMinutes(600);

        //判断答案是否正确
        if (Cache::has($askCode . '_timu')) {
            $timu = Cache::get($askCode . '_timu');
            $answerArray = explode(";",$timu[$index]['correctAnswer']);
            if($answer >= 'A' && $answer <= 'D') {
                $answerInfo['answer'] = $timu[$index][$answer];
            }else{
                $answerInfo['answer'] = $answer;
            }
            // var_dump($answerArray);
            // echo $answerInfo['answer'];
            // echo in_array($answerInfo['answer'],$answerArray);
            if (in_array($answerInfo['answer'],$answerArray)) {
                $answerInfo['isCorrect'] = "YES";
            } else {
                $answerInfo['isCorrect'] = "NO";
            }
        }

        //判断学生是否存在
        if (Cache::has($askCode . '_stu')) {
            $stuall = Cache::get($askCode . '_stu');
            if(isset($stuall[$stuid]))
            {
                $hasStudent = 1;
            }
        }

        //判断学生是否答过题,答过则返回已答
        if ($hasStudent == 1) {
            if (Cache::has($askCode . '_answer_' . $index)) {
                $allAnswer = Cache::get($askCode . '_answer_' . $index);
                $hasAnswer = 1;
                if(isset($allAnswer[$stuid]))
                {
                    $hasAnswer = 0;
                }else{
                    $allAnswer[$stuid] = $answerInfo;
                    Cache::put($askCode . '_answer_' . $index, $allAnswer, $expiresTime);
                }
//                if($hasAnswer == 0) {
  //                  return response()->json(["state" => '2', "error" => '你已经回答过了!', "mode"=>$mode]);
    //            }
            } else {
                //$state = 'no cache,cache add';
                $allAnswer[$stuid] = $answerInfo;
                Cache::add($askCode . '_answer_' . $index, $allAnswer, $expiresTime);
            }
            return response()->json(["state" => '1', "correct" => $answerInfo, "mode"=>$mode]);
        } else {
            return response()->json(["state" => '0', "error" => '学生不存在!', "mode"=>$mode]);
        }
    }

}
